package com.itstherules.jmsunit.loaders;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import com.itstherules.jmsunit.loaders.FileLoader;

public class ResourceFileLoader implements FileLoader {

	private static final String ROOT_PATH = "com/itstherules/jmsunit/";
	
	public String load(String file) throws FileNotFoundException {
		String separator = System.getProperty("line.separator");
		String line = "";
		StringBuffer buffer = new StringBuffer();
		String fileName = ROOT_PATH + file;
		InputStream stream = ResourceFileLoader.class.getClassLoader().getResourceAsStream(fileName);
		if(stream==null) { throw new FileNotFoundException(fileName + " is not found"); }
		BufferedReader bufferedReader= new BufferedReader(new InputStreamReader(stream));
		try {
			while ((line = bufferedReader.readLine())!=null) {
				buffer.append(line);
				buffer.append(separator);
			}
		} catch (IOException e) {
		}
		if(buffer.length() <= separator.length()){
			return buffer.toString();
		}
		return buffer.substring(0, buffer.length()-separator.length());
	}

}
